package com.seelyn.ddvideo.service;

import com.seelyn.ddvideo.event.SystemEventPublisher;
import com.seelyn.ddvideo.spider.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.Date;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/20
 */
@Component
public class SpiderService {

    @Autowired
    private VideoService videoService;
    @Autowired
    private SystemEventPublisher eventPublisher;
    @Autowired
    private Environment environment;

    public void startQQSpider(int page) {
        if (page <= 0) {
            page = 0;
        }
        Spider spider = Spider.create(new QQVideoPageProcessor(eventPublisher, videoService));
        int pageNum = 0;
        for (int i = page; i >= 0; i--) {
            pageNum = page * 30;
            //http://list.video.qq.com/fcgi-bin/list_common_cgi?otype=json&novalue=1&platform=1&version=10000&intfname=web_vip_tv_new&tid=687&appkey=c8094537f5337021&appid=200010596&type=2&sourcetype=1&itype=-1&iyear=-1&sort=19&pagesize=30&offset=60&callback=
            spider.addUrl("http://list.video.qq.com/fcgi-bin/list_common_cgi?otype=json&novalue=1&platform=1&version=10000&intfname=web_vip_cartoon_new&tid=687&appkey=c8094537f5337021&appid=200010596&type=3&sourcetype=1&sort=19&pagesize=30&offset=" + pageNum + "&callback=jqcallback&_=" + new Date().getTime());
            spider.addUrl("http://list.video.qq.com/fcgi-bin/list_common_cgi?otype=json&novalue=1&platform=1&version=10000&intfname=web_vip_tv_new&tid=687&appkey=c8094537f5337021&appid=200010596&type=2&sourcetype=1&itype=-1&iyear=-1&sort=19&pagesize=30&offset=" + pageNum + "&callback=jqcallback&_=" + new Date().getTime());
            spider.addUrl("http://list.video.qq.com/fcgi-bin/list_common_cgi?otype=json&novalue=1&platform=1&version=10000&intfname=web_vip_movie_new&tid=687&appkey=c8094537f5337021&appid=200010596&type=1&sourcetype=1&itype=-1&iyear=-1&iarea=-1&iawards=-1&sort=19&pagesize=30&offset=" + pageNum + "&callback=jqcallback&_=" + new Date().getTime());
        }

        spider.setEmptySleepTime(2);
        spider.run();
    }

    public void startYouKuSpider(int page) {
        if (page <= 0) {
            page = 1;
        }
        Spider spider = Spider.create(new YoukuVideoPageProcessor(eventPublisher, videoService));
        for (int i = page; i >= 1; i--) {
            spider.addUrl("http://vip.youku.com/ajax/filter/filter_data?tag=10005&pl=30&pt=1&ar=0&mg=0&y=0&cl=0&o=1&pn=" + page);//电影
            spider.addUrl("http://vip.youku.com/ajax/filter/filter_data?tag=10006&pl=30&pt=1&ar=0&mg=0&y=0&o=1&pn=" + page);//电视剧
            spider.addUrl("http://vip.youku.com/ajax/filter/filter_data?tag=10007&pl=30&pt=1&ve=0&ar=0&mg=0&y=0&ag=0&o=1&pn=" + page);//动漫
        }

        spider.setEmptySleepTime(2);
        spider.run();
    }

    public void startKuyunSpider(int page) {
        if (page <= 0) {
            page = 1;
        }
        Spider spider = Spider.create(new KuyunVideoPageProcessor(videoService, eventPublisher, environment.getProperty("spring.upload.dir"), environment.getProperty("spring.upload.site")));
        for (int i = page; i >= 1; i--) {
            spider.addUrl("http://ziyuanpian.com/?m=vod-type-id-1-pg-" + i + ".html");
            spider.addUrl("http://ziyuanpian.com/?m=vod-type-id-2-pg-" + i + ".html");
            spider.addUrl("http://ziyuanpian.com/?m=vod-type-id-4-pg-" + i + ".html");
        }
        spider.setEmptySleepTime(2);
        spider.run();
    }

    public void startM3u8Spider(int page) {
        if (page <= 0) {
            page = 1;
        }
        Spider spider = Spider.create(new M3U8VideoPageProcessor(videoService, eventPublisher, environment.getProperty("spring.upload.dir"), environment.getProperty("spring.upload.site")));
        for (int i = page; i >= 1; i--) {
            spider.addUrl("http://www.zuidazy.com/?m=vod-type-id-1-pg-" + i + ".html");
            spider.addUrl("http://www.zuidazy.com/?m=vod-type-id-2-pg-" + i + ".html");
            spider.addUrl("http://www.zuidazy.com/?m=vod-type-id-4-pg-" + i + ".html");
        }
        spider.setEmptySleepTime(2);
        spider.run();
    }

}
